<!--
 * @Author: 刘十三
 * @Date: 2024-02-23 15:49:35
 * @LastEditors: 刘十三
 * @LastEditTime: 2024-03-01 09:32:50
 * @FilePath: \NM-dev-1.3.1\dev\pages\internal\linkage\todo\components\arrangeTaskInformation.vue
 * @Description: 任务派发-任务信息
-->

<template>
  <view class="form-main">
    <u-form class="u-form-m" :model="taskInfo" :error-type="['border-bottom']" label-width="auto" label-position="top" ref="uForm">
      <u-form-item label="任务类型" prop="assignTypeCode" :required="isEdit == 'edit'" :right-icon="isEdit == 'edit' ? 'arrow-right' : ''">
        <u-input placeholder="请选择" v-model="plannStatusName" :disabled="isEdit == 'view'" @click="clickCodeHandle"></u-input>
      </u-form-item>
      <u-form-item label="调查单位" prop="transactOrgId" :required="isEdit == 'edit'" :right-icon="isEdit == 'edit' ? 'arrow-right' : ''">
        <u-input placeholder="请选择" v-model="orgSelectName" :disabled="isEdit == 'view'" @click="clickOrgHandle"></u-input>
      </u-form-item>
      <u-form-item label="任务安排日期" prop="arrangeDate" :required="isEdit == 'edit'" :right-icon="isEdit == 'edit' ? 'arrow-right' : ''">
        <u-input placeholder="请选择" v-model="taskInfo.arrangeDate" :disabled="true" @click="clickDatekHandle('arrangeDate')"></u-input>
      </u-form-item>
      <u-form-item label="任务计划完成日期" prop="planFinishDate" :required="isEdit == 'edit'" :right-icon="isEdit == 'edit' ? 'arrow-right' : ''">
        <u-input placeholder="请选择" v-model="taskInfo.planFinishDate" :disabled="true" @click="clickDatekHandle('planFinishDate')"></u-input>
      </u-form-item>
      <u-form-item label="任务标题" prop="tdmTitle" :required="isEdit == 'edit'">
        <u-input placeholder="任务类型+调查单位" v-model="taskInfo.tdmTitle" :disabled="isEdit == 'view'"></u-input>
      </u-form-item>
      <u-form-item label="调查内容" prop="checkDistributedContentDtoList" :required="isEdit == 'edit' && required" v-if="showCloumns.length > 0">
        <u-input placeholder="请选择" v-model="taskInfo.checkDistributedContentDtoList" :disabled="isEdit == 'view'" style="display: none"></u-input>
        <u-checkbox-group :disabled="isEdit == 'view'" @change="changeDtoList">
          <u-checkbox v-model="item.checked" v-for="(item, index) in showCloumns" :key="index" :name="item.name">{{ item.label }}</u-checkbox>
        </u-checkbox-group>
      </u-form-item>
      <u-form-item label="调查内容描述" prop="taskRemark" :required="isEdit == 'edit'">
        <u-input placeholder="请输入" v-model="taskInfo.taskRemark" type="textarea" height="100" :auto-height="true" :disabled="isEdit != 'edit'" maxlength="500" />
      </u-form-item>
      <u-form-item label="附件" :border-bottom="false">
        <view v-if="isEdit == 'view' && fileList.length == 0" style="padding-top: 10px;margin-left: 5px">
          <u-icon name="attach" color="#2979ff" size="28"></u-icon>
          <span style="font-size: 14px;color: #393B3D;">未上传</span>
        </view>
        <m-upload :deletable="isEdit != 'view'" :disabled="isEdit == 'view'" :maxCount="picIds" width="150rpx" height="150rpx" :file-list="fileList" :multiple="true" index="doorwayUpload" :uploadParam="{ fiGroupCode: $FILE_GROUP_TYPE.l_diversification_attr, bizId: tdmId }" upApiUrl="uploadBase64ForApp" ref="filePicUpload" @on-list-change="handleUpdateFile" />
      </u-form-item>
    </u-form>
    <!-- 日历 -->
    <u-picker mode="time" v-model="showTimeVisable" :params="params" :safe-area-inset-bottom="true" @confirm="changeDate"></u-picker>
    <!-- 任务类型选择 -->
    <u-select v-model="plannStatusVisabled" mode="single-column" :safe-area-inset-bottom="true" :list="plannStatusOptions" @confirm="hanldeConfirmPlanStatus" value-name="bcbCode" label-name="bcbName"></u-select>

    <!-- 组织机构选择 -->
    <u-popup mode="bottom" height="80%" v-model="orgSelectShow">
      <org-select :cleanParentOrg="true" @choose="handlerChooseOrg" :isHybrid="false" limitNodeType="03"></org-select>
    </u-popup>
  </view>
</template>

<script>
// taskDistributed: "task_distributed", // 审批人流程
import mUpload from "@/components/m-upload.vue";
import linkageService from "@/service/internal/linkeage.service";
import orgSelect from "@/components/org-select.vue";
import commonService from "@/service/common/common.service";
import moment from "moment";
export default {
  components: { orgSelect, mUpload },
  name: "arrangeTaskInformation",
  props: {
    isEdit: {
      type: String,
      default: "edit",
    },
    tdmId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      taskInfo: {
        tdmId: "", // 业务id
        assignTypeCode: "", // 任务类型
        transactOrgId: "", // 调查单位
        arrangeDate: "", // 任务安排日期
        tdmTitle: "", // 任务标题
        planFinishDate: "", // 任务计划完成日期
        checkDistributedContentDtoList: "", // 调查内容
        taskRemark: "", // 调查内容描述
      },
      picIds: 10,
      showTimeVisable: false,
      showTimeType: "arrangeDate", // arrangeDate || planFinishDate
      plannStatusName: "",
      required: true,
      plannStatusOptions: [],
      plannStatusVisabled: false,
      params: {
        year: true,
        month: true,
        day: true,
        hour: false,
        minute: false,
        second: false,
      },
      fileList: [],
      orgSelectShow: false,
      orgSelectName: "",
      showCloumns: [],
      rules: {
        assignTypeCode: [
          {
            required: true,
            message: "请选择任务类型",
            trigger: ["blur", "change"],
          },
        ],
        transactOrgId: [
          {
            required: true,
            message: "请选择调查单位",
            trigger: ["blur", "change"],
          },
        ],
        arrangeDate: [
          {
            required: true,
            message: "请选择任务安排日期",
            trigger: ["blur", "change"],
          },
        ],
        planFinishDate: [
          {
            required: true,
            message: "请选择任务计划完成日期",
            trigger: ["blur", "change"],
          },
        ],
        tdmTitle: [
          {
            required: true,
            message: "请选择任务类型和调查单位",
            trigger: ["blur", "change"],
          },
        ],
        checkDistributedContentDtoList: [
          {
            required: true,
            message: "请选择调查内容",
            trigger: ["blur", "change"],
          },
        ],
        taskRemark: [
          {
            required: true,
            message: "请输入调查内容描述",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  watch: {
    plannStatusName: {
      deep: true,
      handler(val) {
        this.combinationValue();
      },
    },
    orgSelectName: {
      deep: true,
      handler(val) {
        this.combinationValue();
      },
    },
  },
  mounted() {
    if (this.isEdit == "edit") {
      this.$refs.uForm.setRules(this.rules);
      this.taskInfo.tdmId = this.tdmId;
      let list = this.$u.dic.getByDictCode("I_TASK_DISTRIBUTED_TYPE") || [];
      this.plannStatusOptions = list;
    } else {
      commonService.goFiles(this.tdmId, this.$FILE_GROUP_TYPE.l_diversification_attr).then((rs) => {
        if (rs.success) {
          if (rs.data.length > 0) {
            this.fileList = rs.data.map((item) => {
              return {
                fileId: item.gfiId,
                gfiId: item.gfiId,
              };
            });
          }
        } else {
          this.$u.toast(rs.msg);
        }
      });
    }
  },
  methods: {
    checkForm() {
      let self = this;
      return new Promise((resolve, reject) => {
        self.$refs.uForm.validate((valid) => {
          if (valid) {
            let form = Object.assign({}, self.taskInfo);
            if (self.taskInfo.checkDistributedContentDtoList) {
              form.checkDistributedContentDtoList = self.taskInfo.checkDistributedContentDtoList.split(",");
            } else {
              form.checkDistributedContentDtoList = [];
            }
            resolve(form);
          } else {
            reject(0);
          }
        });
      });
    },
    // 计算任务标题
    combinationValue() {
      if (this.plannStatusName && this.orgSelectName) {
        this.taskInfo.tdmTitle = this.plannStatusName + this.orgSelectName;
      } else {
        this.taskInfo.tdmTitle = "";
      }
    },
    changeDate(value) {
      this.taskInfo[`${this.showTimeType}`] = value.year + "-" + value.month + "-" + value.day;
      this.showTimeVisable = false;
      this.$refs.uForm.validate();
    },
    getCurDate() {
      return moment().format("YYYY-MM-DD");
    },
    getCurTime() {
      return moment().format("YYYY-MM-DD hh:MM:ss");
    },
    hanldeConfirmPlanStatus(list) {
      let self = this;
      self.plannStatusName = list[0].label;
      self.taskInfo.assignTypeCode = list[0].value;
      self.plannStatusVisabled = false;
      let params = {
        assignTypeCode: list[0].value,
      };
      self.taskInfo.checkDistributedContentDtoList = "";
      self.$u.loading("数据加载中...");
      linkageService
        .getListForAssignTypeCode(params)
        .then((res) => {
          self.$u.hideLoading();
          if (res.success && res.code == 200) {
            self.showCloumns = res.data.map((item) => {
              return {
                label: item.ticName,
                name: item.ticId,
                checked: false, // 默认未选中
                assignTypeCode: item.assignTypeCode,
                assignTypeName: item.assignTypeName,
              };
            });
          }
        })
        .catch((err) => {
          self.$u.hideLoading();
        });
    },
    handlerChooseOrg(orgs) {
      let self = this;
      if (orgs.length == 0) {
        self.$u.toast("请选择组织机构");
        return;
      }
      self.taskInfo.transactOrgId = orgs[0].id;
      self.orgSelectName = orgs[0].name;
      self.orgSelectShow = false;

      let params = {
        tdmId: self.tdmId,
        transactOrgId: self.taskInfo.transactOrgId,
      };
      self.$u.loading("数据加载中...");
      linkageService
        .getCheckBaseInfoFlag(params)
        .then((res) => {
          self.$u.hideLoading();
          if (res.success && res.code == 200) {
            self.required = res.bean;
            self.rules.checkDistributedContentDtoList[0].required = res.bean;
            self.$emit("isFieldsValue", self.required);
          }
        })
        .catch((err) => {
          self.$u.hideLoading();
        });
    },
    changeDtoList(list) {
      let a = list.length > 0 ? list.join(",") : "";
      this.taskInfo.checkDistributedContentDtoList = a;
    },
    clickCodeHandle() {
      if (this.isEdit == "edit") {
        this.plannStatusVisabled = true;
      }
    },
    clickOrgHandle() {
      if (this.isEdit == "edit") {
        this.orgSelectShow = true;
      }
    },
    clickDatekHandle(type) {
      if (this.isEdit == "edit") {
        this.showTimeVisable = true;
        this.showTimeType = type;
      }
    },
    handleUpdateFile(list) {
      if (list.length > 0) {
        this.fileList = [...list];
      } else {
        this.fileList = [];
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.u-form-m {
  padding: 0px 5px;
}
</style>
